/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sip;

import java.net.DatagramPacket;
import javamail.conn.udpSocket;

/**
 *
 * @author sriram
 */
public class sipUAReceive extends Thread {
    private static final boolean debugEnabled = true;
    private int port = 0;
    public static sipMessageAcceptor acceptor = null;
    udpSocket sock = null;

    public sipUAReceive(udpSocket sock) {
        this.sock = sock;
        this.port = javamail.main.glblConfig.getSipPort();
    }

    @Override
    public void run() {
        DatagramPacket temp = null;
        String msg = null;
        if(sock==null || sock.error==true) {
            if(debugEnabled)
                System.out.println("sipUAReceive::run-Unable to run Receive UA as the socket is null");
            return;
        }
        // Start the sipMessageAcceptor
        acceptor = new sipMessageAcceptor();
        acceptor.start();
        if(debugEnabled)
            System.out.println("sipUAReceive::run-Started Acceptor thread");
        
        while (true) {
            if((temp = sock.readPkt()) != null) {
                msg = new String(temp.getData());
            }
            if(debugEnabled) {
                System.out.println("sipUAReceive::run-Received a new packet!");
                System.out.println("From: " + temp.getAddress() + ", Port:" + temp.getPort());
            }
            if(acceptor!=null) {
                synchronized(acceptor) {
                    acceptor.queue(msg);
                }
            }
        }
    }

}

